/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "StdAfx.h"
#include "afxcview.h"
#include ".\phantomlistview.h"

CPhantomListView::CPhantomListView(void) : CPhantomWindow(String("SysListView32"))
{
}

CPhantomListView::~CPhantomListView(void)
{
}

int CPhantomListView::FunctionProcessor(SSI_FunctionAPI& theAPI)
{
	CListView* View = (CListView*)aWnd;
	CListCtrl* Ctrl;
	Ctrl = &(View->GetListCtrl());

	if(theAPI.m_FunctionName == "GetCount"){
		CDataInt i;
		i.Data = (SSI_INT64)Ctrl->GetItemCount();
		theAPI.m_ReturnStatus = theAPI.SetReturnData(&i);
  }else if(theAPI.m_FunctionName == "GetItemText"){
    //LVITEM Item;
    SSI_INT64 i = SSI_EXTRACT(CDataInt, 0);
    SSI_INT64 j = SSI_EXTRACT(CDataInt, 1);

    char Buff[MAX_SSI_CHARS];
    //Item.iItem = i;
    //Item.iSubItem = j;
    //Item.mask = LVIF_TEXT;
    //Item.pszText = Buff;
    //Item.cchTextMax = MAX_SSI_CHARS;
		CDataString s;
    if(Ctrl->GetItemText(i, j, Buff, MAX_SSI_CHARS) >= 0){
      s.Data = Buff;
    }else{
      P_MAKEERR2(ERR_P01575, i, j);
      return 1;
    }
		theAPI.m_ReturnStatus = theAPI.SetReturnData(&s);
  }else if(theAPI.m_FunctionName == "GetColumnCount"){
    CHeaderCtrl* pHeaderCtrl = Ctrl->GetHeaderCtrl();
    CDataInt i;
    if (pHeaderCtrl != NULL){
      i.Data = pHeaderCtrl->GetItemCount();
    }else{
      i.Data = 0;
    }
 		theAPI.m_ReturnStatus = theAPI.SetReturnData(&i);
  }else if(theAPI.m_FunctionName == "GetColumnText"){
    SSI_INT64 i = SSI_EXTRACT(CDataInt, 0);
    CHeaderCtrl* pHeaderCtrl = Ctrl->GetHeaderCtrl();
    CDataString s;
    if (pHeaderCtrl != NULL){
      HDITEM hd;
      char Buff[MAX_SSI_CHARS];
      hd.mask = HDI_TEXT;
      hd.pszText = Buff;
      hd.cchTextMax = MAX_SSI_CHARS;
      int cnt = pHeaderCtrl->GetItemCount();
      if((i >= cnt) || (i < 0)){
        P_MAKEERR1(ERR_P01576, i);
        return 1;
      }
      pHeaderCtrl->GetItem(i, &hd);
      s.Data = Buff;
    }else{
      P_MAKEERR0(ERR_P01578);
      return 1;
    }
		theAPI.m_ReturnStatus = theAPI.SetReturnData(&s);
  }else if(theAPI.m_FunctionName == "Select"){
    SSI_INT64 i = SSI_EXTRACT(CDataInt, 0);
    if(!Ctrl->SetItemState(i, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED)){
      P_MAKEERR1(ERR_P01579, i);
      return 1;
    }

  //}else if(theAPI.m_FunctionName == "GetItemBounds"){
  //  SSI_INT64 i = SSI_EXTRACT(CDataInt, 0);
  //  RECT r;
  //  
  //  CVariable RetVar;
  //  CDataInt aData;
	//	RetVar.SetType(CDataInt::GetRegisteredType());
	//	DimNode aDim = new_array_node();
	//	
  //  if(Ctrl->GetItemRect(i, &r, LVIR_BOUNDS)){
	//	  for(int ctr = 0; ctr < 4; ctr++){
	//		  set_array_node(aDim, ctr);  // Because Dims are one based ... need to make multi-dimensional stuff more intuitive if it is to be available to general user.
  //      if(ctr == 0){
  //        aData.Data = r.left;
  //      }else if(ctr == 1){
  //        aData.Data = r.right;
  //      }else if(ctr == 2){
  //        aData.Data = r.top;
  //      }else if(ctr == 3){
  //        aData.Data = r.bottom;
  //      }
	//		  RetVar.SetData((CData*)&aData, aDim);
	//	  }
	//	  theAPI.SetReturnData(&RetVar);
	//	  del_array_node(aDim);
  //  }else{
  //    P_MAKEERR1(ERR_P01577, i);
  //  }

	}else{
		return CPhantomWindow::FunctionProcessor(theAPI);
	}
	return 1;
}

CPhantomWindow* CPhantomListView::MakeWindow(void)
{
	return new CPhantomListView();
}

